package com.masterclient.app.modules.main;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.eoner.baselib.utils.activity.UtilActivity;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.common.config.Config;
import com.masterclient.app.R;
import static android.widget.Toast.LENGTH_SHORT;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/23 17:25
 * 功能说明：
 */

public class PrivacyDialog extends Dialog  {

    private static String URL ="https://mda.3dintime.com/agreement/privacy";

    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.tv_disagree)
    TextView tvDisagree;
    @BindView(R.id.iv_check)
    ImageView ivCheck;

    private Context context;
    private boolean isSelected = false;

    public PrivacyDialog(@NonNull Context context, AgreementPrivacy agreementPrivacy) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.agreementPrivacy = agreementPrivacy;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_privacy);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(false);
        SpannableString spanableInfo = new SpannableString(tvContent.getText().toString().trim());
        spanableInfo.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(0xFF7399E5);
                ds.setFlags(Paint.ANTI_ALIAS_FLAG);
            }

            @Override
            public void onClick(@NonNull View widget) {
                Intent intent = new Intent(context, PrivacyWebActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("url",URL);
                bundle.putString("title", "隐私政策");
                intent.putExtras(bundle);
                context.startActivity(intent);            }
        }, 44, 50, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(spanableInfo);
        tvContent.setHighlightColor(context.getResources().getColor(android.R.color.transparent));
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    @OnClick({R.id.tv_agreement, R.id.tv_disagree, R.id.iv_check, R.id.tv_privacy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_agreement:
                if (!isSelected){
                    Toast toast = Toast.makeText(context, "请先勾选同意中华大师汇隐私政策", LENGTH_SHORT);
                    toast.show();
                    return;
                }
                dismiss();
                SPUtils.getInstance().put(Config.APP_PRIVACY_UPDATE_VERSION, true);
                agreementPrivacy.onAgreement();
                break;
            case R.id.tv_disagree:
                dismiss();
                UtilActivity.finishAll();
                System.exit(0);
                break;
            case R.id.iv_check:
                if (isSelected){
                    isSelected = false;
                    ivCheck.setImageResource(R.mipmap.radio_rectangle_default);
                }else {
                    isSelected = true;
                    ivCheck.setImageResource(R.mipmap.radio_rectangle_selected);
                }
                break;
            case R.id.tv_privacy:
                Intent intent = new Intent(context, PrivacyWebActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("url",URL);
                bundle.putString("title", "隐私政策");
                intent.putExtras(bundle);
                context.startActivity(intent);                break;
            default:
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
            UtilActivity.finishAll();
            System.exit(0);
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private AgreementPrivacy agreementPrivacy;


    public interface AgreementPrivacy {
        void onAgreement();
    }

}
